package com.payment.ktb.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.R;
import com.payment.ktb.constants.ConstantsCustomer;
import com.payment.ktb.model.CustomerManageEntity;
import com.payment.ktb.model.MessageNoticeEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.StringUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by admin on 2017/9/22.
 */

public class CustomerManageAdapter extends BaseAdapter {

    private Context mContext;
    private List<CustomerManageEntity> listdatas;

    public CustomerManageAdapter(Context mContext, List<CustomerManageEntity> listdatas) {
        this.mContext = mContext;
        this.listdatas = listdatas;
    }

    public CustomerManageAdapter() {
    }

    @Override
    public int getCount() {
        return listdatas.size();
    }

    @Override
    public Object getItem(int i) {
        return listdatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = View.inflate(mContext, R.layout.adapter_customermanage_item,null);
            viewHolder.tv_customermanage_userAccount = (TextView) view.findViewById(R.id.tv_customermanage_userAccount);
            viewHolder.tv_customermanage_regDate = (TextView) view.findViewById(R.id.tv_customermanage_regDate);
            viewHolder.tv_customermanage_userPhone = (TextView) view.findViewById(R.id.tv_customermanage_userPhone);
            viewHolder.tv_customermanage_userStatus = (TextView) view.findViewById(R.id.tv_customermanage_userStatus);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (TextUtils.isEmpty(listdatas.get(i).getUserAccount())) {
            viewHolder.tv_customermanage_userAccount.setText(StringUtils.handelPhone(listdatas.get(i).getUserPhone()));
        }else {
            viewHolder.tv_customermanage_userAccount.setText(listdatas.get(i).getUserAccount());
        }

        viewHolder.tv_customermanage_regDate.setText(listdatas.get(i).getRegDate());


        viewHolder.tv_customermanage_userPhone.setText(StringUtils.handelPhone(listdatas.get(i).getUserPhone()));
        final String phone = listdatas.get(i).getUserPhone();
        viewHolder.tv_customermanage_userPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialogUtils.showAlertDialog(mContext, StringUtils.handelPhone(phone), "取消", "呼叫", new AlertInterface() {
                    @Override
                    public void confirm(Dialog alertDialog) {
                        mContext.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone)));
                    }
                });


            }
        });



        if (listdatas.get(i).getUserStatus() == ConstantsCustomer.PASS.getCode()) {
            viewHolder.tv_customermanage_userStatus.setTextColor(mContext.getResources().getColor(R.color.green));
        }else if (listdatas.get(i).getUserStatus() == ConstantsCustomer.FAIL.getCode()) {
            viewHolder.tv_customermanage_userStatus.setTextColor(mContext.getResources().getColor(R.color.red));
        }else if (listdatas.get(i).getUserStatus() == ConstantsCustomer.INIT.getCode()) {
            viewHolder.tv_customermanage_userStatus.setTextColor(mContext.getResources().getColor(R.color.orange));
        }else {
            viewHolder.tv_customermanage_userStatus.setTextColor(mContext.getResources().getColor(R.color.text666666));
        }
        viewHolder.tv_customermanage_userStatus.setText(ConstantsCustomer.getStatus((listdatas.get(i).getUserStatus())));

        return view;
    }


    class ViewHolder {

        TextView tv_customermanage_userAccount;
        TextView tv_customermanage_regDate;
        TextView tv_customermanage_userPhone;
        TextView tv_customermanage_userStatus;

    }

}
